const express = require('express');
const router = express.Router();
const sql = require("../sql/index");
const Cart = require("../sql/collection/Cart");
const Pro = require("../sql/collection/Pro");
const utils = require("../utils/index")
const uuid = require("node-uuid")

router.get('/', function (req, res, next) {
    res.send()
});
/**
* @api {post} /api/cart/add 加入购物车
* @apiName PostApiCartAdd
* @apiGroup Cart
* 
* @apiParam { String } userid 用户ID
* @apiParam { String } proid 商品ID
* @apiParam { Number } num 商品数量，默认为1
* @apiParam { String } token token
* 
* @apiSuccessExample { json } Success-Response:
        res.send({
            code: '200',
            message: '加入购物车成功'
        })
        res.send({
            code: '10011',
            message: '加入购物车失败'
        })
* @apiSampleRequest /api/cart/add
* @apiVersion 0.0.0
*/
// 加入购物车
router.post('/add', function (req, res, next) {
    let { userid, proid, num } = req.body;
    num = num * 1 || 1
    // 购物车是否已存在该商品
    // 不存在插入商品信息
    // 存在，数量+1
    // 验证token
    utils.verifyToken(req, res).then(() => {
        sql.find(Cart, { userid, proid }, { _id: 0}).then( data => {
            if (data.length === 0) {
                const insertData = {
                    cartid: 'cart_' + uuid.v1(),
                    userid: userid,
                    proid: proid,
                    num,
                    flag: 1
                }
                sql.insert(Cart, insertData).then(() => {
                    res.send({
                        code: '200',
                        message: '加入购物车成功'
                    })
                })
            } else {
                sql.update(Cart, { userid, proid }, {$inc: { num }}).then(() => {
                    res.send({
                        code: '200',
                        message: '加入购物车成功'
                    })
                })
            }
        }, (err) => {
            res.send({
                code: '10011',
                message: '加入购物车失败' + err
            })
        })
    })
    
});
/**
* @api {post} /api/cart/cartdata 获取购物车数据
* @apiName PostApiCartData
* @apiGroup Cart
* 
* @apiParam { String } userid 用户ID
* 
* @apiSuccessExample { json } Success-Response:
        res.send({
            code: '200',
            message: '请求购物车数据成功',
            data: proList
        })
        res.send({
            code: '10012',
            message: '购物车没有有商品'
        })
        res.send({
            code: '10013',
            message: '获取购物车数据失败' + err
        })
* @apiSampleRequest /api/cart/cartdata
* @apiVersion 0.0.0
*/
// 获取购物车数据
router.post('/cartdata', (req, res, next) => {
    // [{}, {}, {}]
    let { userid } = req.body
    utils.verifyToken(req, res).then(() => {
        sql.find(Cart, { userid }, { _id: 0 }).then(data => {
            if (data.length === 0) {
                res.send({
                    code: '10012',
                    message: '购物车没有有商品'
                })
            } else {
                let arr = []
                data.forEach((val) => {
                    arr.push(sql.find(Pro, { proid: val.proid }, { _id: 0 }))
                })
                Promise.all(arr).then(result => {
                    let proList = []
                    result.forEach((item, index) => {
                        proList.push({
                            cartid: data[index].cartid,
                            proid: data[index].proid,
                            proname: item[0].proname,
                            proimg: item[0].proimg,
                            price: item[0].price,
                            num: data[index].num,
                            flag: data[index].flag
                        })
                    })
                    res.send({
                        code: '200',
                        message: '请求购物车数据成功',
                        data: proList
                    })
                })
            }
        }, (err) => {
            res.send({
                code: '10013',
                message: '获取购物车数据失败' + err
            })
        }) 
    })
})
/**
* @api {post} /api/cart/deletecartdata 删除购物车数据
* @apiName PostApiDeleteCartData
* @apiGroup Cart
* 
* @apiParam { String } userid 用户ID
* @apiParam { String } proid 商品ID
* 
* @apiSuccessExample { json } Success-Response:
        res.send({
            code: '200',
            message: '删除购物车数据'
        })
* @apiSampleRequest /api/cart/deletecartdata
* @apiVersion 0.0.0
*/
// 删除购物车数据
router.post('/deletecartdata', (req, res, next) => {
    const { userid, proid } = req.body;
    sql.delete(Cart, { userid, proid }).then(() => {
        res.send({
            code: '200',
            message: '删除购物车数据'
        })
    })
})
/**
* @api {post} /api/cart/deletecartlistdata 删除购物车确认订单数据
* @apiName PostApiDeleteCartListData
* @apiGroup Cart
* 
* @apiParam { String } userid 用户ID
* @apiParam { String } proid 商品ID
* 
* @apiSuccessExample { json } Success-Response:
        res.send({
            code: '200',
            message: '删除购物车确认订单数据'
        })
* @apiSampleRequest /api/cart/deletecartlistdata
* @apiVersion 0.0.0
*/
// 删除购物车确认订单数据
router.post('/deletecartlistdata', (req, res, next) => {
    const { userid, list } = req.body;
    let arr = [];
    list.forEach(val => {
        arr.push(
            sql.delete(Cart, { userid, proid: val.proid})
        )
    })
    Promise.all(arr).then(() => {
        res.send({
            code: '200',
            message: '删除确认订单商品数据'
        })
    })
})
/**
* @api {post} /api/cart/updata 更新购物车数量
* @apiName PostApiUpdata
* @apiGroup Cart
* 
* @apiParam { String } cartid 购物车ID
* @apiParam { String } proid 商品ID
* @apiParam { Number } num  商品数量
* 
* @apiSuccessExample { json } Success-Response:
        res.send({
            code: '200',
            message: '更新购物车数量'
        })
* @apiSampleRequest /api/cart/updata
* @apiVersion 0.0.0
*/
// 更新购物车数量
router.post('/updata', (req, res, next) => {
    const { cartid, proid, num } = req.body;
    sql.update(Cart, { cartid, proid }, { $set: { num }}).then(() => {
        res.send({
            code: '200',
            message: '更新购物车数量'
        })
    })
})
/**
* @api {post} /api/cart/selectallflag 全选勾选状态
* @apiName PostApiselectAllFlag
* @apiGroup Cart
* 
* @apiParam { String } userid 购物车ID
* @apiParam { Boolean } flag 勾选状态
* 
* @apiSuccessExample { json } Success-Response:
        res.send({
            code: '200',
            message: '更新全选状态'
        })
* @apiSampleRequest /api/cart/selectallflag
* @apiVersion 0.0.0
*/
// 全选勾选状态
router.post('/selectallflag', (req, res, next) => {
    const { userid, flag } = req.body;
    sql.update(Cart, { userid }, { $set: { flag }}, 1).then(() => {
        res.send({
            code: '200',
            message: '更新全选状态'
        })
    })
})
/**
* @api {post} /api/cart/selectflag 单个商品勾选状态
* @apiName PostApiselectFlag
* @apiGroup Cart
* 
* @apiParam { String } cartid 购物车ID
* @apiParam { String } proid 商品ID
* @apiParam { Boolean } flag 勾选状态
* 
* @apiSuccessExample { json } Success-Response:
        res.send({
            code: '200',
            message: '更新单个商品选中状态'
        })
* @apiSampleRequest /api/cart/selectflag
* @apiVersion 0.0.0
*/
// 单个商品勾选状态
router.post('/selectflag', (req, res, next) => {
    const { cartid, proid, flag } = req.body;
    sql.update(Cart, { cartid, proid }, { $set: { flag }}).then(() => {
        res.send({
            code: '200',
            message: '更新单个商品选中状态'
        })
    })
})
module.exports = router;
